﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using DotNet.Common.Command;
using ZLCMS.Business.Shop.Commands.Order;
using ZLCMS.Business.Util;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;
using ZLCMS.Web.Wap.Code;

namespace ZLCMS.Web.MemberUser
{
    public partial class Default : WebMemberPageBase
    {
        public int waitForPay = 0;
        public int waitForReceive = 0;
        protected MemberInfo _memberDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            _memberDb = Master.LoadMember();

            MemberCategory1.MenuIndex = "grzx";

            IList<ShopOrderInfo> orderList = new LoadOrderList().Execute(new LoadOrderListParameter()
            {
                MemberAccount = Session[GlobalSessionString.CurrentMemberAccount].ToString() ,
                PayStatusFilterValue = "-1",
                ShipStatusFilterValue = "-1",
                IsCancelledFilterValue = "0",
                IsConfirmedFilterValue = "-1"
            }).Result.List;

            for (int i = orderList.Count-1; i >= 0;i-- )
            {
                ////if (orderList[i].PayStatus == "0"&&orderList[i].IsCancelled=="0")
                if ((orderList[i].PayStatus == "0" && orderList[i].IsCancelled == "0" && orderList[i].PaymentType != "2" && orderList[i].IsFinished == "0") )
                {
                    waitForPay += 1;
                }
                if ((orderList[i].PayStatus == "1" && orderList[i].PaymentType != "2"&& orderList[i].ShippingStatus == "1" && orderList[i].IsFinished == "0") || (orderList[i].PaymentType == "2"  && orderList[i].ShippingStatus == "1" && orderList[i].IsFinished == "0"))
                {
                    waitForReceive += 1;
                }
                if(i>4)
                {
                    orderList.Remove(orderList[i]);
                }
            }
            rpt_order.DataSource = orderList;
            rpt_order.DataBind();
        }

        #region 绑定订单的产品信息 + protected void rptOrder1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        /// <summary>
        /// 绑定待付款订单的产品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate，

            //HeaderTemplate，，ItemTemplate，SeparatorTemplate）
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rpt_product") as Repeater;//找到里层的repeater对象
                ShopOrderInfo order = (ShopOrderInfo)e.Item.DataItem;//找到分类Repeater关联的数据项 

                LoadOrderDetailResult res = (LoadOrderDetailResult)new LoadOrderDetailCommand().Execute(new LoadOrderDetailParameter() { OrderNo = order.OrderNo });

                rep.DataSource = res.Result;
                rep.DataBind();
            }
        }
        #endregion

        public string GetPaymentName(string id)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopPaymentInfo payment = session.CreateCriteria<ShopPaymentInfo>().Add(Expression.Eq("Id", int.Parse(id))).UniqueResult<ShopPaymentInfo>();
                if (payment != null)
                {
                    return payment.PaymentName;
                }
                else
                {
                    return "";
                }
            }
        }
    }
}
